package com.pkk.penguin.frame.know.java.Javacoreknowledge.lock.impl;

import com.pkk.penguin.frame.know.java.Javacoreknowledge.lock.LockVo;
import com.pkk.penguin.frame.know.java.Javacoreknowledge.lock.Type;
import com.pkk.penguin.frame.know.java.Javacoreknowledge.lock.base.LockThread;
import com.pkk.penguin.frame.know.java.Javacoreknowledge.lock.base.NoLockThread;
import org.junit.Test;

/**
 * @description: synchionzed的同步锁
 * @author: peikunkun
 * @create: 2020-01-13 15:55
 **/
public class Synchionzed {


  @Test
  public void Administrator_18_20200117093720() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_18()_20200117093720】");
    System.out.println("此方法测试描述：【同一个实例对象,当锁住的是一个实例对象时,普通的方法】");

    LockVo lockVo = new LockVo();
    final Thread thread1 = new Thread(new Runnable() {
      @Override
      public void run() {
        LockVo.staticLock();
      }
    }, "simple-1");
    thread1.start();

    final Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        LockVo.staticCommon();
      }
    }, "simple-2");
    thread.start();

    thread.join();
    thread1.join();
  }


  @Test
  @SuppressWarnings("all")
  public void Administrator_20_20200116172000() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_18()_20200116163335】");
    System.out.println("此方法测试描述：【静态方法锁的是一个类实例,一个获取到锁不释放,另一个线程不会获取到锁】");

    //final Type lockType = Type.staticLock;
    //final Type lockType = Type.staticInnerLock;
    final Type lockType = Type.staticInnerObjLock;

    /**
     *锁同一个对象
     */
    final Thread thread1 = new Thread(new Runnable() {
      @Override
      public void run() {
        final LockVo lockVo = new LockVo(lockType);
        LockThread lockThread = LockThread.builder().lockVo(lockVo).threadName("simple-1").build();
        LockThread lockThread1 = LockThread.builder().lockVo(lockVo).threadName("simple-2").build();
        lockThread.start();
        lockThread1.start();

        try {
          lockThread.join();
          lockThread1.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    });
    thread1.setName("main-simple-1");

    /**
     * 锁不同对象
     */
    final Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        LockThread lockThread = LockThread.builder().lockVo(new LockVo(lockType)).threadName("simple-3")
            .build();
        LockThread lockThread1 = LockThread.builder().lockVo(new LockVo(lockType)).threadName("simple-4")
            .build();
        lockThread.start();
        lockThread1.start();

        try {
          lockThread.join();
          lockThread1.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    });
    thread.setName("main-simple-2");
    thread.start();
    thread1.start();

    thread1.join();
    thread.join();
  }


  @Test
  @SuppressWarnings("all")
  public void Administrator_18_20200116164208() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_18()_20200116163335】");
    System.out
        .println("此方法测试描述：【实例方法内部同步锁实例的Object对象,多个对象也就是有多个Object实例,同一个Object对象锁不释放,其他线程获取不到锁,不同的Object对象实例,获取锁不影响】");

    /**
     *锁同一个对象
     */
    final Thread thread1 = new Thread(new Runnable() {
      @Override
      public void run() {
        final LockVo lockVo = new LockVo(Type.innerObjLock);
        LockThread lockThread = LockThread.builder().lockVo(lockVo).threadName("simple-1").build();
        LockThread lockThread1 = LockThread.builder().lockVo(lockVo).threadName("simple-2").build();
        lockThread.start();
        lockThread1.start();

        try {
          lockThread.join();
          lockThread1.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    });
    thread1.setName("main-simple-1");

    /**
     * 锁不同对象
     */
    final Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        LockThread lockThread = LockThread.builder().lockVo(new LockVo(Type.innerObjLock)).threadName("simple-3")
            .build();
        LockThread lockThread1 = LockThread.builder().lockVo(new LockVo(Type.innerObjLock)).threadName("simple-4")
            .build();
        lockThread.start();
        lockThread1.start();

        try {
          lockThread.join();
          lockThread1.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    });
    thread.setName("main-simple-2");
    thread.start();
    thread1.start();

    thread1.join();
    thread.join();
  }


  @Test
  @SuppressWarnings("all")
  public void Administrator_18_20200116163335() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_18()_20200116163335】");
    System.out.println("此方法测试描述：【实例方法内部类锁】");
    System.out.println("无论锁的对象是否为同一个,方法内部锁的是一个类,如何这个线程不释放,其他线程不会获取到锁");

    /**
     *锁同一个对象
     */
    final Thread thread1 = new Thread(new Runnable() {
      @Override
      public void run() {
        final LockVo lockVo = new LockVo(Type.innerLock);
        LockThread lockThread = LockThread.builder().lockVo(lockVo).threadName("simple-1").build();
        LockThread lockThread1 = LockThread.builder().lockVo(lockVo).threadName("simple-2").build();
        lockThread.start();
        lockThread1.start();

        try {
          lockThread.join();
          lockThread1.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    });
    thread1.setName("main-simple-1");

    /**
     * 锁不同对象
     */
    final Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        LockThread lockThread = LockThread.builder().lockVo(new LockVo(Type.innerLock)).threadName("simple-3").build();
        LockThread lockThread1 = LockThread.builder().lockVo(new LockVo(Type.innerLock)).threadName("simple-4").build();
        lockThread.start();
        lockThread1.start();

        try {
          lockThread.join();
          lockThread1.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    });
    thread.setName("main-simple-2");
    thread.start();
    thread1.start();

    thread1.join();
    thread.join();
  }

  @Test
  @SuppressWarnings("all")
  public void Administrator_17_20200116141858() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_17()_20200116141858】");
    System.out.println("此方法测试描述：【实例方法锁,锁同一个实例对象与锁不同实例对象的对比】");
    System.out.println("锁同一个实例对象,一个线程不释放锁,另一个线程无法获取");
    System.out.println("锁不同实例对象,一个线程不释放锁,另一个线程照样可以获取");
    /**
     *锁同一个对象
     */
    final Thread thread1 = new Thread(new Runnable() {
      @Override
      public void run() {
        final LockVo lockVo = new LockVo(Type.lock);
        LockThread lockThread = LockThread.builder().lockVo(lockVo).threadName("simple-1").build();
        LockThread lockThread1 = LockThread.builder().lockVo(lockVo).threadName("simple-2").build();
        lockThread.start();
        lockThread1.start();

        try {
          lockThread.join();
          lockThread1.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    });
    thread1.setName("main-simple-1");

    /**
     * 锁不同对象
     */
    final Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        LockThread lockThread = LockThread.builder().lockVo(new LockVo(Type.lock)).threadName("simple-3").build();
        LockThread lockThread1 = LockThread.builder().lockVo(new LockVo(Type.lock)).threadName("simple-4").build();
        lockThread.start();
        lockThread1.start();

        try {
          lockThread.join();
          lockThread1.join();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    });
    thread.setName("main-simple-2");
    thread.start();
    thread1.start();

    thread1.join();
    thread.join();
  }


  @Test
  @SuppressWarnings("all")
  public void Administrator_16_20200113174500() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_16()_20200113174500】");
    System.out.println("此方法测试描述：【不加锁,多个线程中无论跑的是否为同一个实例,皆不会影响,受CPU调度】");
    final NoLockThread lockThread = new NoLockThread(new LockVo(Type.lock));
    final NoLockThread lockThread1 = new NoLockThread(new LockVo(Type.lock));
    lockThread.start();
    lockThread1.start();

    lockThread.join();
    lockThread1.join();
  }


  @Test
  @SuppressWarnings("all")
  public void Administrator_12_20200113155523() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_12()_20200113155523】");
    System.out.println("此方法测试描述：【使用同一个实例对象去跑方法同步锁,两个线程中其中一个线程持有锁,另一个线程只有等待】");
    final LockVo lockVo = new LockVo(Type.lock);
    final LockThread lockThread = new LockThread(lockVo);
    final LockThread lockThread1 = new LockThread(lockVo);
    lockThread.start();
    lockThread1.start();

    lockThread.join();
    lockThread1.join();
  }


  @Test
  public void Administrator_48_20200113174930() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_48()_20200113174930】");
    System.out.println("此方法测试描述：【使用不同的实例去跑方法同步锁,锁的是实例对象,不同的实例互不影响】");
    final LockThread lockThread = new LockThread(new LockVo(Type.lock));
    final LockThread lockThread1 = new LockThread(new LockVo(Type.lock));
    lockThread.start();
    lockThread1.start();

    lockThread.join();
    lockThread1.join();
  }


}
